package com.bw.user.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.user.mvvm.model.RestPwdModel;
import com.bw.user.mvvm.view.activity.LoginsActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/20 18:28
 */
public class RestPwdViewModel extends BaseViewModel<RestPwdModel> {

    public ObservableField<String> pwd = new ObservableField<>();
    public ObservableField<String> repwd = new ObservableField<>();
    private String username;
    @Override
    public void oncreate() {
        super.oncreate();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity.getMessage().equals("密码修改成功")){
            showMsg("修改成功");
            HashMap<String, Object> map = new HashMap<>();
            map.put(UIField.PAGECLAZZ, LoginsActivity.class);
            startPage(map);
            finishPage();
        }else {
            showMsg("修改失败");
        }
    }

    public void comitClick(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile",username);
        map.put("pwd",pwd.get());
        m.requestRestpwd(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    public void backClick(){
        finishPage();
    }

    @Subscribe(sticky = true)
    public void name(String s){
        username=s;
    }
}
